define(function(require, exports, module) {
	'use strict';
	require('resource/font-awesome-4.7.0/css/font-awesome.min.css');
	/**
	 * 日期格式化
	 */
	Date.prototype.format = function (formatStr) {
		var str = formatStr;
		var Week = ['日', '一', '二', '三', '四', '五', '六'];

		str = str.replace(/yyyy|YYYY/, this.getFullYear());
		str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
		var month = this.getMonth() + 1;
		str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
		str = str.replace(/M/g, this.getMonth());

		str = str.replace(/w|W/g, Week[this.getDay()]);

		str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
		str = str.replace(/d|D/g, this.getDate());

		str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
		str = str.replace(/h|H/g, this.getHours());
		str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
		str = str.replace(/m/g, this.getMinutes());

		str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
		str = str.replace(/s|S/g, this.getSeconds());

		return str;
	};
	
	/**日期转时间戳*/
	function datetime_to_unix(datetime){
		if(datetime){
			var tmp_datetime = datetime.replace(/:/g,'-');
		    tmp_datetime = tmp_datetime.replace(/ /g,'-');
		    var arr = tmp_datetime.split("-");
		    if(!arr[3]){
		    	arr[3] = arr[4] = arr[5] = 0;
		    }
		    var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
		    return parseInt(now.getTime());
		}else{
			return "";
		}
	}
	
	/**时间戳转日期*/
	function unix_to_datetime(unix, format) {
		if(unix){
			var date = new Date(parseInt(unix));
			return date.format(format || "YYYY-MM-dd hh:mm:ss");
		}else{
			return "";
		}
	}
	/**求两个时间的天数差 日期格式为 YYYY-MM-dd*/
	function daysBetween(DateOne,DateTwo){
    	var dt = DateTwo;
    	//360浏览器默认nowdate YYYY年MM月dd日
    	//谷歌                 nowdate YYYY-MM-dd
    	dt = dt.replace("年","-");
    	dt = dt.replace("月","-");
    	dt = dt.replace("日","");
        var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));  
        var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);  
        var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));  
      
        var TwoMonth = dt.substring(5,dt.lastIndexOf ('-'));  
        var TwoDay = dt.substring(dt.length,dt.lastIndexOf ('-')+1);  
        var TwoYear = dt.substring(0,dt.indexOf ('-'));  
      
        var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);   
        return cha;
    }
	
	function showLoadding(msg){
		var msgText = '加载中，请稍后...';
		if (msg) {
			msgText = msg;
		} 
		var myWindow = getMainWindow();
		$(myWindow.document).find('body')
		.append('<div class="loadding common-util-loadding" style="z-index: 10000;position: fixed;top:0;left:0;width:100%;height: 100%;background:black;text-align: center;opacity: 0.50;"><div style="color:#fff;width:200px;padding: 10px;border-radius: 5px;position: absolute;margin-top: -50px;top: 40%;left: 50%;margin-left: -100px;"><i class="fa fa-spinner fa-spin fa-3x fa-fw" style="color:#09f;font-size:70px;"></i><span style="display:block;">'+msgText+'</span></div></div>');
	}
	
	function hideLoadding() {
		var myWindow = getMainWindow();
		$(myWindow.document).find('.common-util-loadding').remove();
	}
	
	//递归和查询子frame的父级frame(name=mainFrame)所在window
	//"name=mainFrame"--->index.js--->loader();
	 var getMainWindow = function(win){
		if(win == window.top){
			return win;
		}
		var myWindow = win || window;
		if(myWindow.frameElement && myWindow.frameElement.name == "mainFrame"){
			return myWindow;
		}else{
			return getMainWindow(myWindow.parent);
		}
	};
	
	//获取月份天数
	function getMonthDays(year, month){
	    var d = new Date(year, month, 0);
	    return d.getDate();
	}

	var Base64 = {
			_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
			encode: function(input) {
				var output = "";
				var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
				var i = 0;
				input = Base64._utf8_encode(input);
				while (i < input.length) {
					chr1 = input.charCodeAt(i++);
					chr2 = input.charCodeAt(i++);
					chr3 = input.charCodeAt(i++);

					enc1 = chr1 >> 2;
					enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
					enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
					enc4 = chr3 & 63;

					if (isNaN(chr2)) {
						enc3 = enc4 = 64;
					} else if (isNaN(chr3)) {
						enc4 = 64;
					}

					output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

				}

				return output;
			},
			decode: function(input) {
				var output = "";
				var chr1, chr2, chr3;
				var enc1, enc2, enc3, enc4;
				var i = 0;
				input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

				while (i < input.length) {
					enc1 = this._keyStr.indexOf(input.charAt(i++));
					enc2 = this._keyStr.indexOf(input.charAt(i++));
					enc3 = this._keyStr.indexOf(input.charAt(i++));
					enc4 = this._keyStr.indexOf(input.charAt(i++));

					chr1 = (enc1 << 2) | (enc2 >> 4);
					chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
					chr3 = ((enc3 & 3) << 6) | enc4;

					output = output + String.fromCharCode(chr1);

					if (enc3 != 64) {
						output = output + String.fromCharCode(chr2);
					}
					if (enc4 != 64) {
						output = output + String.fromCharCode(chr3);
					}
				}

				output = Base64._utf8_decode(output);
				return output;
			},

			_utf8_encode: function(string) {
				string = string.replace(/\r\n/g, "\n");
				var utftext = "";
				for (var n = 0; n < string.length; n++) {
					var c = string.charCodeAt(n);

					if (c < 128) {
						utftext += String.fromCharCode(c);
					}
					else if ((c > 127) && (c < 2048)) {
						utftext += String.fromCharCode((c >> 6) | 192);
						utftext += String.fromCharCode((c & 63) | 128);
					}
					else {
						utftext += String.fromCharCode((c >> 12) | 224);
						utftext += String.fromCharCode(((c >> 6) & 63) | 128);
						utftext += String.fromCharCode((c & 63) | 128);
					}

				}
				return utftext;
			},

			_utf8_decode: function(utftext) {
				var string = "";
				var i = 0, c = 0,c2 = 0, c3 = 0;

				while (i < utftext.length) {
					c = utftext.charCodeAt(i);
					if (c < 128) {
						string += String.fromCharCode(c);
						i++;
					} else if ((c > 191) && (c < 224)) {
						c2 = utftext.charCodeAt(i + 1);
						string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
						i += 2;
					} else {
						c2 = utftext.charCodeAt(i + 1);
						c3 = utftext.charCodeAt(i + 2);
						string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
						i += 3;
					}
				}
				return string;
			}
		};
	//检查设备是否是iOS
	function checkIsIOS() {
	    var browser = {
	        versions: function () {
	            var u = navigator.userAgent, app = navigator.appVersion;
	            return { //移动终端浏览器版本信息 
	                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 
	                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器 
	                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 
	                iPad: u.indexOf('iPad') > -1, //是否iPad 
	            };
	        }(),
	    }
	    if (browser.versions.iPhone || browser.versions.iPad || browser.versions.ios) {
	        return true;
	    }
        return false;
	}
	//检查设备是否是Android
	function checkIsAndroid() {
	    var browser = {
	        versions: function () {
	            var u = navigator.userAgent, app = navigator.appVersion;
	            return { //移动终端浏览器版本信息 
	                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 
	                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器 
	                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 
	                iPad: u.indexOf('iPad') > -1, //是否iPad 
	            };
	        }(),
	    }
	    //if (browser.versions.iPhone || browser.versions.iPad || browser.versions.ios) {
	    //    return false;
	    //}
	    if (browser.versions.android)
	        return true;
	    return false;
	}
	
	
	module.exports = {
		datetime_to_unix: function(datetime) {
			return datetime_to_unix(datetime);
		},
		unix_to_datetime: function(unix, format) {
			return unix_to_datetime(unix, format);
		},
		showLoadding: function(msg) {
			return showLoadding(msg);
		},
		hideLoadding: function() {
			return hideLoadding();
		},
		daysBetween: function(DateOne,DateTwo) {
			return daysBetween(DateOne,DateTwo);
		},
		encode: function(str){
			return Base64.encode(str);
		},
		decode: function(str){
			return Base64.decode(str);
		},
		utf8_encode: function(str){
			return Base64._utf8_encode(str);
		},
		utf8_decode: function(str){
			return Base64._utf8_decode(str);
		},
		getMonthDays: function(year, month) {
			return getMonthDays(year, month);
		},
		checkIsIOS: function() {
			return checkIsIOS();
		},
		checkIsAndroid: function() {
			return checkIsAndroid();
		}
	}
});